<template>
	<div class="newusernormal-main">
		<el-table :data="showData" height="85%" style="margin-left: 2%; width: 96%;">
			<el-table-column prop="userid" label="用户id"></el-table-column>
			<el-table-column prop="username" label="用户名"></el-table-column>
			<el-table-column prop="userphone" label="用户电话"></el-table-column>
			<el-table-column label="操作">
				<template slot-scope="scope" style="display: flex;flex-direction: row; flex-wrap: nowrap;">
					<el-button @click="handleDeRegister(scope.row)" type="text" style="color: red;"
						size="small">注销</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div style="margin-left: 2%;height: 15%;width: 96%;
		display: flex;justify-content: space-between;align-items: center;">
			<el-button @click="lastpage()" size="small">返回第一页</el-button>
			<el-button @click="nextpage()" size="small">下一页</el-button>
		</div>
	</div>
</template>

<script>
	import axios from "axios"
	import webSetInfo from "@/utils/webSetInfo";
	export default {

		data() {
			return {
				showData: [],
				userinfo: {},
				requestType: 2,
				totalNumber: 0
			}
		},
		mounted() {
			this.userinfo = JSON.parse(localStorage.getItem("userinfo"))
			if (this.userinfo == null) {
				this.$router.push({
					path: '/login'
				})
			} else {
				this.getNewInfo()
			}
		},
		methods: {
			handleDeRegister(e) {
				var that = this
				axios({
						url: `${webSetInfo.apiUrl}` + `/deRegister`,
						method: "post",
						data: {
							requestCode: '006',
							userid: that.userinfo.userid,
							token: that.userinfo.token,
							targetuserid: e.userid,
							targetuserpriority: 0,
						},
						headers: {
							'Content-Type': 'application/x-www-form-urlencoded'
						}
					})
					.then(function(response) {
						var data = response.data
						var result = data.result
						if (result == 1) {
							that.$message({
								message: '修改成功',
								type: 'success'
							});
							//重新加载
							that.lastpage()
						} else if (result == 0) {
							that.tokenExpire()
						} else {
							this.$message.error("未知错误")
						}
					})
			},
			getNewInfo() {
				var that = this
				axios({
						url: `${webSetInfo.apiUrl}` + `/getNewInfo`,
						method: "post",
						data: {
							requestCode: '005',
							userid: that.userinfo.userid,
							token: that.userinfo.token,
							beginid: that.totalNumber + 1,
							userpriority: that.requestType
						},
						headers: {
							'Content-Type': 'application/x-www-form-urlencoded'
						}
					})
					.then(function(response) {
						var data = response.data
						var result = data.result
						if (result == 1) {
							if (data.data.length == 0) {
								that.$message.error("暂无更多数据")
							} else {
								that.loadData(data.data)
							}
						} else if (result == 0) {
							that.tokenExpire()
						}
					})
			},
			//token过期
			tokenExpire: function() {
				this.$router.push({
					path: '/login',
				})
			},
			//装载data
			loadData(e) {
				this.showData = e
				this.totalNumber += e.length
			},
			//上一页
			lastpage() {
				this.totalNumber = 0
				this.getNewInfo()
			},
			//下一页
			nextpage() {
				this.getNewInfo()
			}
		}
	}
</script>

<style>
	.newusernormal-main {
		height: 100%;
		width: 100%;
		display: flex;
		flex-direction: column;
	}
</style>
